import numpy as np
import csv
import random
from datetime import datetime
from time import strftime
from dbfpy import dbf
import matplotlib.pyplot as plt
from scipy import stats

'''
read dbf file
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    f = open(inputCSV)
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.DictReader(f, dialect="excel")

    for record in ra:
        #sKey.append('0')
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            sKey.append(int(float(str(record[item]))))

    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def cal_dist(x1, y1, x2, y2):
    temp_dist = (float(x1 - x2))**2 + (float((y1 - y2))**2)
    #print x1, y1, x2, y2, (float(x1 - x2))**2, (float((y1 - y2))**2), temp_dist
    temp_dist = temp_dist**0.5
    #print temp_dist

    return temp_dist

def fivenum(v):
    """Returns Tukey's five number summary (minimum, lower-hinge, median, upper-hinge, maximum) for the input vector, a list or array of numbers based on 1.5 times the interquartile distance"""
    import numpy as np
    from scipy.stats import scoreatpercentile
    try:
        np.sum(v)
    except TypeError:
        print('Error: you must provide a list or array of only numbers')
    q1 = scoreatpercentile(v,25)
    q3 = scoreatpercentile(v,75)
    md = np.median(v)
    return np.min(v), q1, md, q3, np.max(v),

#--------------------------------------------------------------------------
#MAIN
if __name__ == "__main__":
    print "begin at " + getCurTime()
    filePath = 'C:/_DATA/migration_census_2000/STATE/'
    file = filePath + 'census_migration_state_dist.csv'
    
    data = build_data_list(file)

    inoutflowfile = filepath + 'census_migration_state_pop2000.csv'
    inoutflow = build_data_list(inoutflowfile)

    #Centroid_CSV = filePath + 'census_county_centroid.csv'
    #centroid = build_data_list(Centroid_CSV)
    distgraphcsv = filepath + 'graphdistance.csv'
    distgraph = build_data_list(distgraphcsv)

    totalflow = np.sum(data[:,-1])
    expdata = []
    for item in data:
        expdata.append(float(inoutflow[item[1],1])*inoutflow[item[2],0]/totalflow)  

    avgExp = np.average(expdata)
    ajustOb = []
    for ob, exp in zip(data[:,-1], expdata):
        ajustOb.append(ob/(exp/avgExp))

    for item in data:
        item[0] = distgraph[int(item[1]), int(item[2])]
        
    hist_bin = np.zeros(15)
    #output = []
    #sum_0 = 0
    #sum_1 = 0
    #for item in data:
        #temp = int(item[0]/10000)
        #temp = item[0]
        #hist_bin[temp] += item[-1]


    #for dist, flow in zip(data[:,0], expdata):
        #hist_bin[dist] += flow
        
    for dist, flow in zip(data[:,0], data[:,-1]):
        hist_bin[dist] += flow

    #for item in data:
        #dist = int(cal_dist(centroid[item[0], 0], centroid[item[0], 1], centroid[item[1], 0], centroid[item[1], 1]))
        #output.append([dist, item[2]])

    #output = np.array(output)
    #print fivenum(output[:,0])
    '''
    n, bins, patches = plt.hist(data[:,0], max(data[:,0])-1, normed = 1, facecolor = 'green', alpha = 0.75)
    plt.xlabel('graph distance')
    plt.ylabel('frequence')
    plt.title('State-pair Graph Dist. Histogram')
    plt.show()
    
    '''
    #plt.plot(range(15), hist_bin)
    #plt.loglog(range(15), hist_bin)
    xx = range(15)
    yy = hist_bin

    plt.subplot(221)
    plt.scatter(xx[1:], yy[1:])
    plt.plot(xx[1:], yy[1:])
    plt.xlabel('graph distance')
    plt.ylabel('flow volume')
    plt.title('Original Data')
    plt.subplot(222)
    plt.scatter(xx, np.log(yy))
    plt.plot(xx, np.log(yy))
    plt.xlabel('graph distance')
    plt.ylabel('log(flow volume)')
    plt.title('log(y)')
    plt.subplot(223)
    plt.scatter(np.log(xx), np.log(yy))
    plt.plot(np.log(xx), np.log(yy))
    plt.xlabel('log(graph distance)')
    plt.ylabel('log(flow volume)')
    plt.title('log(x), log(y)')
    plt.subplot(224)
    plt.loglog(xx, yy, marker = 'o')
    plt.xlabel('graph distance')
    plt.ylabel('flow volume')
    plt.title('loglog plot')
    #plt.axis()
    plt.xlim(xmin = 0, xmax = 15)
    plt.show()
    
    #fileLoc = filePath + 'census_county_migration_format_dist.csv'
    #np.savetxt(fileLoc, output, delimiter=',', fmt = '%s')

    fileLoc = filePath + 'census_county_migration_format_dist_10k_bin.csv'
    #np.savetxt(fileLoc, hist_bin, delimiter=',', fmt = '%s')

    print "end at " + getCurTime()
    print "========================================================================"  
    
            